import Vue from "vue";
import axios from "axios";
import Qs from "qs";

if (process.env.NODE_ENV !== "development") {
  axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
}

// 请求超时时间
axios.defaults.timeout = 20000;
axios.defaults.withCredentials = true;

// post请求头
axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded;charset=UTF-8";


// axios.defaults.transformRequest = [function (data) {
//   let src = ''
//   for (let item in data) {
//     src += encodeURIComponent(item) + '=' + encodeURIComponent(data[item]) + '&'
//   }
//   return src
// }]

// 响应拦截器
axios.interceptors.response.use(
  response => {
    if (response.status > 199 && response.status < 300) {
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  // 服务器状态码不是200的情况
  error => {
    if (error.response.status) {
      switch (error.response.status) {
        // 其他错误，直接抛出错误提示
        default:
        // Toast({
        //   message: error.response.data.message,
        //   duration: 1500,
        //   forbidClick: true
        // })
      }
      return Promise.reject(error.response);
    }
  }
);

function error(res) {
  return true;
}

function Get(url, params) {
  let sessionId = localStorage.getItem("sessionId");
  axios.defaults.headers.get['Authorization'] =  `JWT ${sessionId}` ;
  axios.defaults.headers.post['Authorization'] =   `JWT ${sessionId}`;
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params
      })
      .then(res => {
        if (error(res)) {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

function Post(url, params) {
  let sessionId = localStorage.getItem("sessionId");
  axios.defaults.headers.get['Authorization'] =  `JWT ${sessionId}` ;
  axios.defaults.headers.post['Authorization'] =  `JWT ${sessionId}`;
  return new Promise((resolve, reject) => {
    axios
      .post(url, params)
      .then(res => {
        if (error(res)) {
          resolve(res.data);
        } else {
          resolve(res.data);
        }
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
function Put(url, params) {
  let sessionId = localStorage.getItem("sessionId");
  axios.defaults.headers.get['Authorization'] =  `JWT ${sessionId}` ;
  axios.defaults.headers.post['Authorization'] =  `JWT ${sessionId}`;
  axios.defaults.headers.put['Authorization'] =  `JWT ${sessionId}`;
  axios.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
  return new Promise((resolve, reject) => {
    axios
      .put(url, Qs.stringify({params}))
      .then(res => {
        if (error(res)) {
          resolve(res.data);
        } else {
          resolve(res.data);
        }
      })
      .catch(err => {
        reject(err.data);
      });
  });
}
function Posts(url,params) {
  return new Promise((resolve, reject) => {
    axios
      .post(url,params,{headers:{
          'token':JSON.parse(localStorage.getItem('user')).sessionId,
        //  "Content-Type":"multipart/form-data;boundary=---WebKitFormBoundaryKPjNOGYtWEjAni5F"
        }
      })
      .then(res => {
        if (error(res)) {
          resolve(res.data);
        } else {
          resolve(res.data);
        }
      })
      .catch(err => {
        reject(err.data);
      });
  });
}

Vue.prototype.$axios = {
  get: Get,
  post: Post,
  put: Put,
  axiosProperty:axios
};

export default {
  get: Get,
  post: Post,
  put: Put,
  posts:Posts
}

Vue.prototype.$request = axios;
